package bot;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FileStorage implements DataStorage{
	
	private File file;
	public static final String split = " ;&: ";
	private	Map<String, String> data;
	
	public FileStorage(File file){
		data = new HashMap<String, String>();
		this.file = file;
		try{
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = null;
			while ((line = reader.readLine()) != null){
				String values[] = line.split(split);
				if (values.length == 39){
					data.put(values[0], line);
				}
				else{
					throw new IllegalArgumentException("data missing " + values.length + " " + line);
				}
			}
		} 
		catch (FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
	}



	@Override
	public boolean addEntry(String url, City city){
		if (data.containsKey(url)){
			return false;
		}
		StringBuilder builder = new StringBuilder(1000);
		builder.append(url);
		builder.append(split);
		builder.append(city.getName());
		builder.append(split);
		builder.append(city.getType());
		builder.append(split);
		builder.append(city.getTotalPopulation());
		builder.append(split);
		builder.append(city.getPercentUnder18());
		builder.append(split);
		builder.append(city.getPercent18to24());
		builder.append(split);
		builder.append(city.getPercent25to44());
		builder.append(split);
		builder.append(city.getPercent45to64());
		builder.append(split);
		builder.append(city.getPercentOver65());
		builder.append(split);
		builder.append(city.getMedianAge());
		builder.append(split);
		builder.append(city.getPercentMale());
		builder.append(split);
		builder.append(city.getPercentFemale());
		builder.append(split);
		builder.append(city.getPercentWhite());
		builder.append(split);
		builder.append(city.getPercentBlack());
		builder.append(split);
		builder.append(city.getPercentNative());
		builder.append(split);
		builder.append(city.getPercentAsian());
		builder.append(split);
		builder.append(city.getPercentPacific());
		builder.append(split);
		builder.append(city.getPercentOther());
		builder.append(split);
		builder.append(city.getPercentTwoOrMore());
		builder.append(split);
		builder.append(city.getPercentHispanic());
		builder.append(split);
		builder.append(city.getHouseholds());
		builder.append(split);
		builder.append(city.getFamilies());
		builder.append(split);
		builder.append(city.getPercentWithChildren());
		builder.append(split);
		builder.append(city.getPercentHusbandAndWife());
		builder.append(split);
		builder.append(city.getPercentMaleNoWife());
		builder.append(split);
		builder.append(city.getPercentFemaleNoHusband());
		builder.append(split);
		builder.append(city.getPercentNonFamily());
		builder.append(split);
		builder.append(city.getPercentLivingAlone());
		builder.append(split);
		builder.append(city.getPercentLivingAloneOver65());
		builder.append(split);
		builder.append(city.getAverageHouseholdSize());
		builder.append(split);
		builder.append(city.getAverageFamilySize());
		builder.append(split);
		builder.append(city.getNumberOfHouses());
		builder.append(split);
		builder.append(city.getAreaLand());
		builder.append(split);
		builder.append(city.getAreaWater());
		builder.append(split);
		builder.append(city.getPopulationEstimate2011());
		builder.append(split);
		builder.append(city.getSumlev());
		builder.append(split);
		builder.append(city.getState());
		builder.append(split);
		builder.append(city.getPlace());
		builder.append(split);
		builder.append(city.getPopulationEstimate2012());
		
		if (builder.toString().split(split).length != 39){
			throw new IllegalArgumentException("Too many or too few of stats when adding entry");
		}
		
		
		builder.append("\n");
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
			writer.write(builder.toString());
			writer.close();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean contains(String url){
		throw new IllegalStateException("not currently implemented");
	}

	@Override
	public String getName(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[1];
	}

	@Override
	public String getType(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[2];
	}
	
	@Override
	public String getTotalPopulation(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[3];
	}

	@Override
	public String getPercentUnder18(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[4];
	}

	@Override
	public String getPercent18to24(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[5];
	}

	@Override
	public String getPercent25to44(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[6];
	}

	@Override
	public String getPercent45to64(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[7];
	}

	@Override
	public String getPercentOver65(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[8];
	}

	@Override
	public String getMedianAge(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[9];
	}

	@Override
	public String getPercentMale(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[10];
	}

	@Override
	public String getPercentFemale(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[11];
	}

	@Override
	public String getPercentWhite(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[12];
	}

	@Override
	public String getPercentBlack(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[13];
	}

	@Override
	public String getPercentNative(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[14];
	}

	@Override
	public String getPercentAsian(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[15];
	}

	@Override
	public String getPercentPacific(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[16];
	}

	@Override
	public String getPercentOther(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[17];
	}

	@Override
	public String getPercentTwoOrMore(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[18];
	}

	@Override
	public String getPercentHispanic(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[19];
	}

	@Override
	public String getHouseholds(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[20];
	}

	@Override
	public String getFamilies(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[21];
	}

	@Override
	public String getPercentWithChildren(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[22];
	}

	@Override
	public String getPercentHusbandAndWife(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[23];
	}

	@Override
	public String getPercentMaleNoWife(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[24];
	}

	@Override
	public String getPercentFemaleNoHusband(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[25];
	}

	@Override
	public String getPercentNonFamily(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[26];
	}

	@Override
	public String getPercentLivingAlone(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[27];
	}

	@Override
	public String getPercentLivingAloneOver65(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[28];
	}

	@Override
	public String getAverageHouseholdSize(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[29];
	}

	@Override
	public String getAverageFamilySize(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[30];
	}

	@Override
	public String getNumberOfHouses(String url){
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[31];
	}

	@Override
	public String getAreaLand(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[32];
	}

	@Override
	public String getAreaWater(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		return values[33];
	}

	@Override
	public String getPopulationEstimate2011(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		if (!values[34].equals("null")){
			return values[34];
		}
		return null;
	}

	@Override
	public String getSumlev(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		if (!values[35].equals("null")){
			return values[35];
		}
		return null;
	}

	@Override
	public String getState(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		if (!values[36].equals("null")){
			return values[36];
		}
		return null;
	}

	@Override
	public String getPlace(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		if (!values[37].equals("null")){
			return values[37];
		}
		return null;
	}

	@Override
	public String getPopulationEstimate2012(String url) {
		String tuple = data.get(url);
		String values[] = tuple.split(split);
		if (!values[38].equals("null")){
			return values[38];
		}
		return null;
	}

}
